<template>
  <el-dialog title="收款" :model-value="dialog" @close="close">
    <el-form :model="formData" :rules="rules" ref="uploadForm" label-width="auto" style="padding: 0 20px" class="Dform">
      <el-form-item label="收款状态" prop="receivedType">
        <el-radio-group v-model="formData.receivedType">
          <el-radio :label="1">全款</el-radio>
          <el-radio :label="2">部分收款</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="收款金额" prop="receivedAmount" v-if="formData.receivedType == 2">
        <el-input v-model="formData.receivedAmount" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit()">提交</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script lang="ts">
import { defineComponent, ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import { orderStatusMap, orderTypeMap, priceModelTypeMap } from '@/config/constants'
import { useUserStore } from '@/stores'
import { proceeds } from "@/api/settlement"
export default defineComponent({
  props: {
    dialog: {
      type: Boolean,
      default: false,
    },
    currentForm: {
      type: Object,
      default: () => { },
    }
  },
  emits: ['update:currentForm', 'update:dialog', 'refresh'],
  setup(props, context) {
    const formData = reactive(props.currentForm)
    const uploadForm = ref<any>(null)
    const userStore = useUserStore()

    const close = () => {
      context.emit('update:dialog', false)
      context.emit('refresh', { ...formData })
      context.emit('update:currentForm', {})
    }

    onMounted(() => {

    })

    const onSubmit = () => {
      uploadForm.value.validate((valid: boolean) => {
        if (valid) {
          proceeds(formData).then((res: any) => {
            if (res.code == 200) {
              ElMessage.success('收款成功')
              close()
            }
          })
        } else {
          ElMessage({
            message: '验证失败',
            type: 'error',
            duration: 1000,
          })
          return false
        }
      })
    }

    const resetForm = () => {
      uploadForm.value.resetFields()
      context.emit('update:currentForm', {})
    }

    return {
      uploadForm,
      formData,
      priceModelTypeMap,
      userStore,
      orderStatusMap,
      orderTypeMap,
      close,
      resetForm,
      onSubmit,
      rules: {
        receivedType: [{ required: true, message: '请选择收款状态', trigger: 'blur' }],
        receivedAmount: [{ required: true, message: '请输入收款金额', trigger: 'blur' }],
      },
    }
  },
})
</script>

<style></style>
